<template>
  <div>
    <el-container>
      <el-header>
        <h2>
          <b>修改密码</b>
        </h2>
      </el-header>
      <el-main>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="旧密码" prop="oldPwd">
            <el-input
              type="password"
              v-model="ruleForm.oldPwd"
              autocomplete="off"
              style="width:50%"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd">
            <el-input
              type="password"
              v-model="ruleForm.newPwd"
              autocomplete="off"
              style="width:50%"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="rePwd">
            <el-input type="password" v-model="ruleForm.rePwd" autocomplete="off" style="width:50%"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import Axios from "axios";
import Qs from "qs";
export default {
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else {
        if (this.ruleForm.newPwd !== "") {
          this.$refs.ruleForm.validateField("newPwd");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.ruleForm.rePwd !== "") {
          this.$refs.ruleForm.validateField("rePwd");
        }
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPwd: "",
        newPwd: "",
        rePwd: "",
      },
      rules: {
        oldPwd: [{ validator: validatePass1, trigger: "blur" }],
        newPwd: [{ validator: validatePass2, trigger: "blur" }],
        rePwd: [{ validator: validatePass3, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("确认修改?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              Axios.post(
                "/tfss/admin/changePwd",
                Qs.stringify({
                  oldPwd: this.ruleForm.oldPwd,
                  newPwd: this.ruleForm.newPwd,
                })
              ).then((r) => {
                if (r.data.code == 200) {
                  this.$message({
                    type: "success",
                    message: "密码修改成功!",
                  });
                  this.$refs[formName].resetFields();
                } else if (r.data.code == 403) {
                  this.$message.error("没有权限");
                } else if (r.data.code == 702) {
                  this.$message.error("原密码错误");
                } else if (r.data.code == 703) {
                  this.$message.error("未登录");
                } else if (r.data.code == 500) {
                  this.$message.error("系统繁忙，请稍后再试");
                } else if (r.data.code == 704) {
                  this.$message.error("登录失效，请重新登录");
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消",
              });
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.el-header,
.el-footer {
  /* background-color: #b3c0d1; */
  color: #333;
  /* text-align: center; */
  line-height: 60px;
}
.el-main {
  /* background-color: #e9eef3; */
  color: #333;
  /* text-align: center; */
  line-height: 160px;
}
</style>